const nodeMediaServer = require('node-media-server')
let mediaServer = null
// let defaultConfig = {
//   rtmp: {port: 1987, chunk_size: 60000, gop_cache: true, ping: 60, ping_timeout: 30},
//   http: {port: 1990, allow_origin: '*'}
// }

/**
 * 创建服务器
 * @param {Number} options/rtmpPort RTMP端口
 * @param {Number} options/httpPort HTTP端口
 */
const createServer = (options) => {
  // 启动服务
  mediaServer = new nodeMediaServer(options)
  mediaServer.run()
}

const start = (options = {}) => {
  let config = {
    rtmp: {port: options.rtmpPort || 1987, chunk_size: 60000, gop_cache: true, ping: 60, ping_timeout: 30},
    http: {port: options.httpPort || 1990, allow_origin: '*'}
  }
  createServer(config)
}

const stop = () => {
  // 关闭media服务器
  mediaServer && mediaServer.stop()
}

// 获取服务器对象
const getServer = () => {
  return mediaServer
}

module.exports = {
  getServer,
  start,
  stop
}
